package com.sakura.controller;


import com.github.pagehelper.PageInfo;
import com.sakura.po.Actor;
import com.sakura.po.BeComment;
import com.sakura.service.BeCommentService;
import com.sakura.util.R;
import org.apache.ibatis.javassist.bytecode.FieldInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
//@RequestMapping(value="/beComment")

public class BeCommentController {

    @Autowired
    private BeCommentService beCommentService;

    @GetMapping("/becommentIndex")
    public String becommentIndex (){
        return "beComment/becommentIndex";

    }

    @GetMapping("/addcomment")
    public String addcomment(){ return"beComment/addcomment"; }


    @ResponseBody
    @RequestMapping("/addBecomment")
    public R addBecomment(@RequestBody BeComment beComment){
        beCommentService.addBecomment(beComment);
        return R.ok();
    }

    /*//解决跨越问题
    @CrossOrigin
    @RequestMapping(value="/addComment",produces = "application/json;charset=utf-8",method = RequestMethod.PUT)
    public int addBecomment(@RequestBody BeComment beComment){
        System.out.println("进入用户添加方法");
        return beCommentService.addBecomment(beComment);
    }*/

    @ResponseBody
    @RequestMapping("/updateBecomment")
    public R updateBecomment(@RequestBody BeComment beComment){
        System.out.println("进入用户修改方法，修改的主键为："+ beComment.getId());
        beCommentService.updateBecomment(beComment);
        return R.ok();
    }

    /*//解决跨越问题
    @CrossOrigin
    @RequestMapping(value = "/updateComment",produces = "application/json;charset=utf-8",method = RequestMethod.PUT)
    public int updateBecomment(@RequestBody BeComment beComment){
        System.out.println("进入用户修改方法，修改的主键为："+ beComment.getId());
        return beCommentService.updateBecomment(beComment);
    }*/

    @ResponseBody
    @RequestMapping("/deleteBecomment")
    public R deleteBecomment(String ids){
        List list
                = Arrays.asList(ids.split(","));
        beCommentService.deleteBecomment(list);
        return R.ok();
    }

    /*//解决跨越问题
    @CrossOrigin
    @RequestMapping(value = "/deleteComment",produces = "application/json;charset=utf-8",method = RequestMethod.GET)
    public int deleteBecomment(int id){
        System.out.println("进入删除用户信息，主键为："+id);
        return beCommentService.deleteBecomment(id);
    }*/

    //根据id查询记录信息
    @GetMapping("/getBecomment")
    public String getBecomment(Model model, Integer id){
        //System.out.println("进入根据主键查找用户信息，主键为："+id);
        BeComment beComment= beCommentService.getBecomment(id);
        model.addAttribute("beComment",beComment);
        return "beComment/updatecomment";
        //return beCommentService.getBecomment(id);
    }
    /*//解决跨越问题
    @CrossOrigin
    @RequestMapping(value = "/getComment",produces = "application/json;charset=utf-8",method = RequestMethod.GET)
    public BeComment getBecomment(int id){
        System.out.println("进入根据主键查找用户信息，主键为："+id);
        return beCommentService.getBecomment(id);
    }*/
    @ResponseBody
    @RequestMapping("/getAll")
    public R getAll(BeComment beComment,
                    @RequestParam(defaultValue = "1") Integer page,
                    @RequestParam(defaultValue = "15") Integer limit){
        PageInfo<BeComment> pageInfo=beCommentService.getAllBeComments(beComment,page,limit);
        return  R.ok("成功",pageInfo.getTotal(),pageInfo.getList());
    }
    /*//解决跨越问题
    @CrossOrigin
    @RequestMapping(value = "/getAll",produces = "application/json;charset=utf-8",method = RequestMethod.PUT)
    public List<BeComment> getAll(@RequestBody BeComment beComment){
        System.out.println("进入用户查询方法");
        System.out.println("用户名:"+beComment.getUsername()+"======ID:"+beComment.getId()+"======评论:"+beComment.getReply());
        List<BeComment> beCommentList=beCommentService.getAllBeComments(beComment);
        System.out.println(beCommentList.size());
        return beCommentList;
    }*/



}
